package com.quanquan.management.service;

import DTO.PageDTO;
import com.alibaba.fastjson.JSONObject;
import com.quanquan.management.dto.SignInRuleDTO;
import com.quanquan.management.request.SignRuleInRequestBody;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import request.PageRequestBody;
import utils.Request;

/**
 * 功能描述:
 *
 * @ClassName: SignInRuleService
 * @Author: xiaoqianh丶
 * @Date: 2019-04-07 21:10
 * @Version: V1.0
 */
@Service
public class SignInRuleService {

    @Value("${module-url.userUrl}")
    private String userUrl;


    public PageDTO<SignInRuleDTO> listSignInRule(PageRequestBody pageRequestBody) {
        String json = Request.postJson(userUrl+"sign_in_rule/query", pageRequestBody);
        JSONObject jsonObject = JSONObject.parseObject(json);
        Integer resCode = jsonObject.getInteger("resCode");
        PageDTO<SignInRuleDTO> pageDTO = null;
        if (0 == resCode) {
            PageDTO data = jsonObject.getJSONObject("data").toJavaObject(PageDTO.class);
            return data;
        }
        return pageDTO;
    }

    public boolean addSignRule(SignRuleInRequestBody requestBody) {

        if (null == requestBody) {
            return false;
        }
        String json = Request.postJson(userUrl + "sign_in_rule/add", requestBody);
        JSONObject jsonObject = JSONObject.parseObject(json);
        Integer resCode = jsonObject.getInteger("resCode");
        if (0 == resCode) {

                return true;

        }
        return false;
    }

    public SignInRuleDTO findById(String id) {
        SignRuleInRequestBody requestBody = new SignRuleInRequestBody();
        requestBody.setId(id);
        String json = Request.postJson(userUrl + "sign_in_rule/query_by_id", requestBody);
        JSONObject jsonObject = JSONObject.parseObject(json);
        Integer resCode = jsonObject.getInteger("resCode");
        SignInRuleDTO floorDTO = null;
        if (0 == resCode) {
            JSONObject data = jsonObject.getJSONObject("data");
            floorDTO = data.toJavaObject(SignInRuleDTO.class);
        }
        return floorDTO;
    }

    public boolean update(SignRuleInRequestBody requestBody) {
        if (null == requestBody) {
            return false;
        }
        String json = Request.postJson(userUrl + "sign_in_rule/update_sign_in_rule", requestBody);
        JSONObject jsonObject = JSONObject.parseObject(json);
        Integer resCode = jsonObject.getInteger("resCode");
        if (0 == resCode) {
            return true;
        }
        return false;
    }

    public boolean delete(String id) {
        SignRuleInRequestBody requestBody = new SignRuleInRequestBody();
        requestBody.setId(id);
        String json = Request.postJson(userUrl + "sign_in_rule/remove_sign_in_rule", requestBody);
        JSONObject jsonObject = JSONObject.parseObject(json);
        Integer resCode = jsonObject.getInteger("resCode");
        return resCode == 0;
    }
}
